<template>
  <div>
    <!-- <img alt="Vue logo" src="./assets/logo.png" />
    <h1>{{ count }}</h1>
    <h2>{{ double }}</h2>
    <button @click="increase">点击+1</button>
    <ul>
      <li v-for="number in numbers" :key="number">{{ number }}</li>
    </ul>
    <h3>{{ person.name }}</h3>
    <button @click="updateGreeting">更新标题</button>
    <p>===={{ greetings }}====</p>
    <p>位置X:{{ x }}</p>
    <p>位置y:{{ y }}</p>

    <div>
      <h1 v-if="loading">Loading...</h1>
      <img v-if="loaded" :src="result.message" />
    </div> -->
    <button @click="openModal">Open Modal</button>
    <modal :isOpen="modalIsOpen" @close-modal="onModalClose">My modal!!!!!</modal>
  </div>
</template>

<script lang="ts">
// 重用和把一些变量组合在一起
// ref和toRefs响应式
import {
  ref,
  toRefs,
  computed,
  reactive,
//   onMounted,
//   onUpdated,
//   onUnmounted,
//   onRenderTriggered,
  watch,
} from "vue";
import useMousePosition from "./hooks/useMousePosition";
import useURLLoader from "./hooks/useURLLoader";
import Modal from "./components/Modal.vue";
interface DataProps {
  count: number;
  double: number;
  increase: () => void;
  numbers: number[];
  person: { name?: string };
}

interface DogResult {
  message: string;
  status: string;
}
export default {
  name: "App",
  components: {
    Modal,
  },
  setup() {
    // const count = ref(0)
    // const double = computed(() => {
    //     return count.value * 2
    // })
    // const increase = () => {
    //     count.value++;
    // }
    /**
     * 生命周期钩子
     */
    // onMounted (() => {
    //     console.log('mounted');
    // })
    // onUpdated (() => {
    //     console.log('updated');
    // })
    // onRenderTriggered ((event) => {
    //     console.log(event);
    // })
    const data: DataProps = reactive({
      count: 0,
      increase: () => {
        data.count++;
      },
      double: computed(() => data.count * 2),
      numbers: [0, 1, 2],
      person: {},
    });
    data.numbers[0] = 5;
    data.person.name = "xszi";
    const { x, y } = useMousePosition();
    const { result, loading, loaded } = useURLLoader<DogResult>(
      "https://dog.ceo/api/breeds/image/random"
    );
    watch(result, () => {
      if (result.value) {
        console.log("value:", result.value.message);
      }
    });
    const greetings = ref("");
    const updateGreeting = () => {
      greetings.value += "hello";
    };

    watch([greetings, data], () => {
      document.title = "updated" + greetings.value;
    });
    const refData = toRefs(data);
    const modalIsOpen = ref(false)
    const openModal = () => {
        modalIsOpen.value = true;
    }
    const onModalClose = () => {
        modalIsOpen.value = false;
    }
    return {
      // count,
      // increase,
      // double
      // data
      ...refData,
      greetings,
      updateGreeting,
      x,
      y,
      result,
      loading,
      loaded,
      modalIsOpen,
      openModal,
      onModalClose
    };
  },
  // beforeUnmounted
  // unmounted
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>
